﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;

namespace Runda.EShop.SaaS.SysManage
{
    public partial class UserList : BasePage
    {
        public override string FunctionID
        {
            get
            {
                return "Func5010";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid(ConstHelper.PAGE_INIT_INDEX);
            }
        }

        private void BindGrid(int PageIndex)
        {
            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = null; 

            int RecordCount = 0;
            List<SysUserEntity> list = new SystemBll().GetAll<SysUserEntity>(parms, "CreatedOn DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvUserList.DataSource = list;
            gvUserList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lnkbtnEdit = e.Row.FindControl("lnkbtnEdit") as LinkButton;
                LinkButton lnkbtnEditPass = e.Row.FindControl("lnkbtnEditPass") as LinkButton;
                SysUserEntity entity = e.Row.DataItem as SysUserEntity;
                if (entity.Account == "Admin")
                {
                    LinkButton lnkbtnDelete = e.Row.FindControl("lnkbtnDelete") as LinkButton;
                    lnkbtnDelete.Visible = false;
                    lnkbtnEdit.Visible = false;
                }
                else
                {
                    lnkbtnEdit.Attributes.Add("onclick", "OnUserAdd('UserAdd.aspx?UserID=" + entity.UserID + "');return false;");
                }
                lnkbtnEditPass.Attributes.Add("onclick", "OnPasswordEdit('ChangePassword.aspx?UserID=" + entity.UserID + "');return false;"); 
            }
        }

        protected void gvUserList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SystemBll bll = new SystemBll();
            bll.LogicDelete<SysUserEntity>(" and UserID='" + gvUserList.DataKeys[e.RowIndex]["UserID"].ToString() + "'");
            BindGrid(ConstHelper.PAGE_INIT_INDEX);

            DoOperationLog(EnumHelper.OperationType.Delete, "修改用户，ID:" + gvUserList.DataKeys[e.RowIndex]["UserID"].ToString());
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindGrid(ConstHelper.PAGE_INIT_INDEX);
        }
    }
}